﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace Beeldbankmvc.Helpers
{
    public static class ImageManipulation
    {
        // scaling method // replaced by old scaling method
        /*
        public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
        {
            //a holder for the result
            Bitmap result = new Bitmap(width, height);

            //use a graphics object to draw the resized image into the bitmap
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                //draw the image into the target bitmap
                graphics.DrawImage(image, 0, 0, result.Width, result.Height);
            }

            //return the resulting bitmap
            return result;
        }
        */

        public static byte[] ReadBitmap2ByteArray(Bitmap myBitmapImage)
        {

            MemoryStream stream = new MemoryStream();
            myBitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return stream.ToArray();
        }

        public static System.Drawing.Bitmap CreateThumbnail(string lcFilename, int Width, int Height)
        {
            try
            {
                return GenerateImageThumbnail(lcFilename, Width, Height);
            }
            catch
            {
                return null;
            }
        }


        public static System.Drawing.Bitmap GenerateImageThumbnail(Stream streamImage, int nMaxWidth, int nMaxHeight)
        {
            Image oImage = Image.FromStream(streamImage);

            return GenerateImageThumbnail(oImage, nMaxWidth, nMaxHeight);
        }

        public static System.Drawing.Bitmap GenerateImageThumbnail(string sImagePath, int nMaxWidth, int nMaxHeight)
        {
            Image oImage = Image.FromFile(sImagePath, true);

            return GenerateImageThumbnail(oImage, nMaxWidth, nMaxHeight);
        }

        public static System.Drawing.Bitmap GenerateImageThumbnail(Image oImage, int nMaxWidth, int nMaxHeight)
        {
            float fRatio = 1;
            int nWidth = oImage.Width;
            int nHeight = oImage.Height;

            //calculate the thumb image size if needed
            if (oImage.Width > nMaxWidth || oImage.Height > nMaxHeight)
            {
                fRatio = ((float)oImage.Height) / ((float)oImage.Width);
                nWidth = nMaxWidth;
                nHeight = Convert.ToInt32(nWidth * fRatio);
            }

            //create the thumbnail ans set it’s settings
            Image oThumbnail = new Bitmap(nWidth, nHeight);
            Graphics oGraphic = Graphics.FromImage(oThumbnail);

            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;

            oGraphic.DrawImage(oImage, 0, 0, nWidth, nHeight);
            // Free the resources and the file lock
            oImage.Dispose();

            return new Bitmap(oThumbnail);
        }


        public static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }


    }
}